import { BaseView, UIOrder } from "../../mvc/base_view";
import { UIComponent } from "../../mvc/ui_component";

class CardItem extends UIComponent{
    card_type:number;

    constructor(fairy_root){
        super(fairy_root);
    }

    SetInfo(card_type:number, click_callback:VoidCallback){
        this.card_type = card_type;
        this.SetSelectedIndex("card_face", 1);
        this.GetGText("name").text = config.card[this.card_type.toString()].name;
        this.GetGButton("click_btn").onClick(()=>{
            click_callback()
        });
    }
    
    Selected(is_select:boolean){
        if(is_select){
            this.SetScale(1.5, 1.5);
        }else{
            this.SetScale(1.0, 1.0);
        }
    }

    DeleteMe(){
        
    }
}

export class ChooseCardView extends BaseView{
    card_list:Array<CardItem>;
    select_card:CardItem;
    constructor(){
        super();
        this.ui_package = "main";
        this.layout_name = "ui_choose_card_view";
        this.order = UIOrder.MainUp;
    }

    OpenViewCallback(...args: any[]){
        let close_callback = args[0];
        this.card_list = new Array<CardItem>();
        let card_type_list:Array<number> = [2,4,5,6,7,8,9,10,11,12];
        card_type_list.forEach(card_type => {
                let card = new CardItem(this.GetCom("card_" + card_type));
                card.SetInfo(card_type, () => {
                    if(this.select_card != null){
                        this.select_card.Selected(false);
                    }
                    this.select_card = card;
                    card.Selected(true);
                });
                this.card_list.push(card);
        });

        this.GetGButton("ok_btn").onClick(()=>{
            if(this.select_card != null){
                close_callback(this.select_card.card_type);
                this.Close();
            }
        });

        this.GetGGraph("bg").onClick(()=>{
            close_callback();
            this.Close();
        });
    }
}